//
//  MSRemindSettingVC.m
//  MS
//
//  Created by Yuan on 13-4-14.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSRemindSettingVC.h"
#import "UIColor+ColorUtils.h"
#import "DataPickerSelectView.h"

@interface MSRemindSettingVC ()<UITableViewDataSource,UITableViewDelegate,DataPickerSelectViewDelegate>
{
    UITableView *listTableView;
    NSMutableArray *dataSources;
    
    NSInteger lastSelectHour;
    NSInteger selectHour;
}
@property(nonatomic,retain) UITableView *listTableView;
@end

@implementation MSRemindSettingVC
@synthesize listTableView;

- (id)init
{
    self = [super init];
    
    if (self)
    {
        selectHour = 1;
        
        dataSources = [[NSMutableArray alloc]init];
        
        NSMutableArray *section_one = [[NSMutableArray alloc]init ];
        for (int i = 0; i < 4; i++)
        {
            NSString *title = @"";
            switch (i) {
                case 0:
                    title = HYLocalizedString(@"私信推送");
                    break;
                case 1:
                    title = HYLocalizedString(@"评论");
                    break;
                case 2:
                    title = HYLocalizedString(@"系统消息");
                    break;
                case 3:
                    title = HYLocalizedString(@"新粉丝");
                    break;
                default:
                    break;
            }
            NSMutableDictionary *item = [NSMutableDictionary dictionaryWithObjectsAndKeys:title,@"title",@"1",@"on", nil];
            [section_one addObject:item];
        }
        [dataSources addObject:section_one];
        
        NSMutableArray *section_two = [[NSMutableArray alloc]init];
        for (int  i = 0 ; i < 2; i++)
        {
            NSString *title = @"";
            switch (i)
            {
                case 0:
                    title = HYLocalizedString(@"预约提醒");
                    break;
                case 1:
                    title = HYLocalizedString(@"预约提醒时间");
                    break;
                default:
                    break;
            }
            NSMutableDictionary *item = [NSMutableDictionary dictionaryWithObjectsAndKeys:title,@"title",@"1",@"on",@"1",@"hour", nil];
            [section_two addObject:item];  
        }
        [dataSources addObject:section_two];
        
    }
    
    return self;
}

-(void)loadView
{
    [super loadView];
    listTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    listTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
    listTableView.backgroundColor = [UIColor clearColor];
    listTableView.backgroundView = nil;
    listTableView.delegate = self;
    listTableView.dataSource = self;
    [self.view addSubview:self.listTableView];
}


- (void)viewDidLoad
{
    [self setTitle:HYLocalizedString(@"消息提醒设置") showBackButton:YES];
    [self setRightItem:HYLocalizedString(@"保存")];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)configureViews:(BOOL)animation
{
    [super configureViews:animation];
}


-(void)rightBarButtonItemAction:(UIButton *)btn
{
    [MSTool showAlert:@"保存成功！"];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [dataSources count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[dataSources objectAtIndex:section] count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        UISwitch *sw = [[UISwitch alloc]init];
        sw.onTintColor = [UIColor colorWithString:@"#f376a0"];
        cell.accessoryView =  sw;
        
        if (indexPath.section==1)
        {
            if (indexPath.row == 1)
            {
                UITextField *txtField = [[UITextField alloc]initWithFrame:CGRectZero];
                DataPickerSelectView *inputView=[[DataPickerSelectView alloc]initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, 260.f) withTitle:HYLocalizedString(@"设置预约提醒时间") inPutViewSuperView:txtField style:DataPickerSelectViewTypeCommen];
                inputView.delegate=self;
                txtField.tag = 100;
                txtField.clearButtonMode=UITextFieldViewModeNever;
                txtField.inputView = inputView;
                [cell.contentView addSubview:txtField];
            }
        }
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary *item = [[dataSources objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"title"];
    cell.detailTextLabel.text = @"";
    ((UISwitch *)(cell.accessoryView)).on = [[item objectForKey:@"on"] boolValue];
       
    if (indexPath.section==1)
    {
        if (indexPath.row == 1)
        {
            cell.detailTextLabel.text  = [NSString stringWithFormat:@"%d小时",selectHour];
            cell.accessoryView = nil;
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==1)
    {
        if (indexPath.row == 1)
        {
            UITextField *txtField =  (UITextField *)[[tableView cellForRowAtIndexPath:indexPath].contentView viewWithTag:100];
            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 1;  i < 25; i++)
            {
                NSString *data = [NSString stringWithFormat:@"%d小时",i];
                [arr addObject:data];
            }
            DataPickerSelectView *inputView = (DataPickerSelectView *)txtField.inputView;
            if(arr)[inputView reloadData:arr selectedIndex:selectHour-1];
            [txtField becomeFirstResponder];
        }
    }
}



#pragma #
#pragma DataPickerSelectViewDelegate methord

-(void)dataPickerSelectViewDidCancel:(DataPickerSelectView *)selectView inPutViewSuperView:(UIView *)view
{
    [view resignFirstResponder];
    selectHour = lastSelectHour;
}

-(void)dataPickerSelectViewDidDone:(DataPickerSelectView *)selectView inPutViewSuperView:(UIView *)view
{
    [view resignFirstResponder];
    lastSelectHour = selectHour;
    [listTableView reloadData];
}

-(void)dataPickerSelectView:(DataPickerSelectView *)selectView selectIndex:(NSInteger)row withInfo:(NSObject *)info inPutViewSuperView:(UIView *)view style:(DataPickerSelectViewType)style
{
//    UITextField *renewDateTxt = (UITextField *)view;
//    renewDateTxt.text = (NSString *)info;
    selectHour = row+1;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
